Micron Document
NexusPi Git Node

Node / mirrors / microReticulum_Firmware / files / WebSocketConsole.cpp

Displaying Raw • Download

WebSocketConsole.cpp f0aee0671e84b627f90883c0f1fca8f0d4dbc9da (f0aee067) Text, 4.55 KB

Tff7b72#Tff7b72if defined(ENABLE_WEBSOCKETS) && __has_include(<WiFi.h>)

Tff7b72#Tff7b72include T8b949e"WebSocketConsole.h"
Tff7b72#Tff7b72include T8b949e"WebSocketServer.h"

Tff7b72#Tff7b72include T8b949e<cstdint>
Tff7b72#Tff7b72include T8b949e<cstddef>

T8b949e// KISS frame boundary. Defined inline here rather than including Framing.h
T8b949e// because that header defines several globals at file scope (command,
T8b949e// IN_FRAME, frame_len, ESCAPE) — including it from this second TU would
T8b949e// cause multiple-definition link errors. The byte value is the KISS
T8b949e// standard and isn't going to change.
Tff7b72constexpr Tffa657uint8_t Te6edf3FEND Tff7b72= T79c0ff0xC0Tb4b4b4;

T8b949e// Provided by RNode_Firmware.ino — pushes one byte into serialFIFO when
T8b949e// space is available, drops it on the floor otherwise. Mirrors the
T8b949e// behavior of the other inbound paths in buffer_serial().
Tff7b72extern Ta5d6ff"Ta5d6ffCTa5d6ff" Tffa657void Te6edf3serial_fifo_pushTb4b4b4(Tffa657uint8_t Te6edf3byteTb4b4b4)Tb4b4b4;

Tff7b72namespace Tb4b4b4{

T8b949e// Max single KISS frame we'll emit over WS. Has to cover the largest
T8b949e// Provisioning response (msgpack-encoded schema for all registered
T8b949e// namespaces) plus KISS escaping overhead — schemas exceed 1 KiB on
T8b949e// builds with LORA_TRANSPORT registered, and any future namespace
T8b949e// additions only push it higher. The WS framing already supports 16-bit
T8b949e// ext-len, so this cap can grow up to 0xFFFF before the wire protocol
T8b949e// would need work. If a frame ever exceeds this, on_serial_write logs
T8b949e// "OUTBOUND FRAME DROPPED" and the in-flight frame is discarded.
Tff7b72constexpr Tffa657size_t Te6edf3TX_BUF_CAP Tff7b72= T79c0ff4096Tb4b4b4;

Te6edf3WebSocketServerTff7b72* Te6edf3g_server Tff7b72= Tffa657nullptrTb4b4b4;
Tffa657uint8_t Te6edf3g_tx_bufTb4b4b4[Te6edf3TX_BUF_CAPTb4b4b4]Tb4b4b4;
Tffa657size_t Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Tffa657bool Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4; T8b949e// true between an opening FEND and
T8b949e// the next FEND that completes a
T8b949e// non-empty frame

Tffa657void Td2a8ffon_ws_messageTb4b4b4(Tff7b72const Tffa657uint8_tTff7b72* Te6edf3dataTb4b4b4, Tffa657size_t Te6edf3lenTb4b4b4, Tffa657bool T8b949e/*is_text*/Tb4b4b4,
Tffa657voidTff7b72* T8b949e/*ctx*/Tb4b4b4) Tb4b4b4{
T8b949e// Hand each byte of the WS frame to the KISS parser. The browser is
T8b949e// expected to send one full KISS frame per binary WS frame, but the
T8b949e// serial_poll() KISS parser is byte-stream-driven — it doesn't care
T8b949e// about our WS frame boundaries.
Tff7b72for Tb4b4b4(Tffa657size_t Te6edf3i Tff7b72= T79c0ff0Tb4b4b4; Te6edf3i Tff7b72< Te6edf3lenTb4b4b4; Tff7b72+Tff7b72+Te6edf3iTb4b4b4) Tb4b4b4{
Te6edf3serial_fifo_pushTb4b4b4(Te6edf3dataTb4b4b4[Te6edf3iTb4b4b4]Tb4b4b4)Tb4b4b4;
Tb4b4b4}
Tb4b4b4}

Tffa657void Td2a8ffflush_outbound_frameTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3g_tx_len Tff7b72=Tff7b72= T79c0ff0Tb4b4b4) Tff7b72returnTb4b4b4;
Tff7b72if Tb4b4b4(Te6edf3g_server Tff7b72&Tff7b72& Te6edf3g_serverTff7b72-Tff7b72>Te6edf3connectedTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3g_serverTff7b72-Tff7b72>Te6edf3send_binaryTb4b4b4(Te6edf3g_tx_bufTb4b4b4, Te6edf3g_tx_lenTb4b4b4)Tb4b4b4;
Tb4b4b4}
Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Tb4b4b4}

Tb4b4b4} T8b949e// namespace

Tff7b72namespace T7ee787ws_console Tb4b4b4{

Tffa657void Td2a8ffinitTb4b4b4(Tffa657uint16_t Te6edf3portTb4b4b4, Tffa657bool Te6edf3bind_publicTb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3g_serverTb4b4b4) Tff7b72returnTb4b4b4;
Te6edf3g_server Tff7b72= Tff7b72new Te6edf3WebSocketServerTb4b4b4(Te6edf3portTb4b4b4, Te6edf3bind_publicTb4b4b4)Tb4b4b4;
Te6edf3g_serverTff7b72-Tff7b72>Te6edf3on_messageTb4b4b4(Tff7b72&Te6edf3on_ws_messageTb4b4b4)Tb4b4b4;
Te6edf3g_serverTff7b72-Tff7b72>Te6edf3beginTb4b4b4(Tb4b4b4)Tb4b4b4;
Tb4b4b4}

Tffa657void Td2a8ffserviceTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3g_serverTb4b4b4) Te6edf3g_serverTff7b72-Tff7b72>Te6edf3serviceTb4b4b4(Tb4b4b4)Tb4b4b4;
T8b949e// Drop any outbound buffer if the client went away mid-frame — the
T8b949e// next FEND from serial_write() will start a fresh frame.
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3client_attachedTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Te6edf3g_tx_len Tff7b72> T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4;
Tb4b4b4}
Tb4b4b4}

Tffa657void Td2a8ffon_serial_writeTb4b4b4(Tffa657uint8_t Te6edf3byteTb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3g_serverTb4b4b4) Tff7b72returnTb4b4b4;
T8b949e// We don't drop bytes when no client is attached; we just keep the
T8b949e// buffer empty by never appending. Cheap and avoids the cost of
T8b949e// formatting frames that go nowhere.
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3g_serverTff7b72-Tff7b72>Te6edf3connectedTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4;
Tff7b72returnTb4b4b4;
Tb4b4b4}

Tff7b72if Tb4b4b4(Te6edf3byte Tff7b72=Tff7b72= Te6edf3FENDTb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3g_collecting Tff7b72&Tff7b72& Te6edf3g_tx_len Tff7b72> T79c0ff1Tb4b4b4) Tb4b4b4{
T8b949e// Frame end: append the closing FEND and emit.
Tff7b72if Tb4b4b4(Te6edf3g_tx_len Tff7b72< Te6edf3TX_BUF_CAPTb4b4b4) Te6edf3g_tx_bufTb4b4b4[Te6edf3g_tx_lenTff7b72+Tff7b72+Tb4b4b4] Tff7b72= Te6edf3byteTb4b4b4;
Te6edf3flush_outbound_frameTb4b4b4(Tb4b4b4)Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4;
Tb4b4b4} Tff7b72else Tb4b4b4{
T8b949e// Frame start (or a run of idle FENDs). Reset the buffer
T8b949e// with a single leading FEND and wait for the first data
T8b949e// byte.
Te6edf3g_tx_bufTb4b4b4[T79c0ff0Tb4b4b4] Tff7b72= Te6edf3FENDTb4b4b4;
Te6edf3g_tx_len Tff7b72= T79c0ff1Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657trueTb4b4b4;
Tb4b4b4}
Tff7b72returnTb4b4b4;
Tb4b4b4}

Tff7b72if Tb4b4b4(Tff7b72!Te6edf3g_collectingTb4b4b4) Tb4b4b4{
T8b949e// Mid-stream byte with no preceding FEND — shouldn't happen
T8b949e// under correct KISS, but be lenient: drop it.
Tff7b72returnTb4b4b4;
Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3g_tx_len Tff7b72< Te6edf3TX_BUF_CAPTb4b4b4) Tb4b4b4{
Te6edf3g_tx_bufTb4b4b4[Te6edf3g_tx_lenTff7b72+Tff7b72+Tb4b4b4] Tff7b72= Te6edf3byteTb4b4b4;
Tb4b4b4} Tff7b72else Tb4b4b4{
T8b949e// Frame overflows our buffer. Drop the in-flight frame; the
T8b949e// next FEND will start fresh.
Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4;
Tb4b4b4}
Tb4b4b4}

Tffa657bool Td2a8ffclient_attachedTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72return Te6edf3g_server Tff7b72&Tff7b72& Te6edf3g_serverTff7b72-Tff7b72>Te6edf3connectedTb4b4b4(Tb4b4b4)Tb4b4b4;
Tb4b4b4}

Tffa657void Td2a8ffshutdownTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3g_serverTb4b4b4) Tff7b72returnTb4b4b4;
Te6edf3g_serverTff7b72-Tff7b72>Te6edf3shutdownTb4b4b4(Tb4b4b4)Tb4b4b4;
Tff7b72delete Te6edf3g_serverTb4b4b4;
Te6edf3g_server Tff7b72= Tffa657nullptrTb4b4b4;
Te6edf3g_tx_len Tff7b72= T79c0ff0Tb4b4b4;
Te6edf3g_collecting Tff7b72= Tffa657falseTb4b4b4;
Tb4b4b4}

Tb4b4b4} T8b949e// namespace ws_console

Tff7b72#Tff7b72endif T8b949e// ENABLE_WEBSOCKETS && __has_include(<WiFi.h>)

Served by rngit 1.5.2 - Generated in 0.08s